package org.jash.exam.view

import android.app.DatePickerDialog
import android.graphics.Color
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.exam.databinding.ActivityExamListBinding
import com.example.lib_base.mvvm.BaseActivity
import dagger.hilt.android.AndroidEntryPoint
import org.jash.exam.ExamViewModel
import org.jash.exam.adapter.ExamAdapter
import org.jash.exam.entity.ExamEntity
import java.util.*

@AndroidEntryPoint
@Route(path = "/exam/ExamListActivity")
class ExamListActivity : BaseActivity<ActivityExamListBinding, ExamViewModel>() {
    private lateinit var adapter: ExamAdapter
    private var dateRange: Pair<String?, String?> = null to null
    private var allList: List<ExamEntity> = emptyList()
    override fun initView() {
        binding.back.setOnClickListener { finish() }
        adapter = ExamAdapter(emptyList())
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        binding.recyclerView.adapter = adapter
        vm.examlist(1)

        // 日期选择逻辑
        binding.etDateRange.setOnClickListener {
            val calendar = Calendar.getInstance()
            DatePickerDialog(this, { _, year, month, dayOfMonth ->
                val start = String.format("%04d-%02d-%02d", year, month + 1, dayOfMonth)
                DatePickerDialog(this, { _, y2, m2, d2 ->
                    val end = String.format("%04d-%02d-%02d", y2, m2 + 1, d2)
                    binding.etDateRange.setText("$start ~ $end")
                    dateRange = start to end
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show()
            }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show()
        }
        binding.all.setOnClickListener {
            adapter.setData(allList)
            binding.all.setTextColor(Color.RED)
            binding.nor.setTextColor(Color.BLACK)
            binding.end.setTextColor(Color.BLACK)
            binding.zt.setTextColor(Color.BLACK)
            binding.qq.setTextColor(Color.BLACK)
        }
        binding.nor.setOnClickListener {
            val filtered = allList.filter { it.status == "正常" }
            adapter.setData(filtered)
            binding.all.setTextColor(Color.BLACK)
            binding.nor.setTextColor(Color.RED)
            binding.end.setTextColor(Color.BLACK)
            binding.zt.setTextColor(Color.BLACK)
            binding.qq.setTextColor(Color.BLACK)
        }
        binding.end.setOnClickListener {
            val filtered = allList.filter { it.status == "迟到" }
            adapter.setData(filtered)
            binding.all.setTextColor(Color.BLACK)
            binding.nor.setTextColor(Color.BLACK)
            binding.end.setTextColor(Color.RED)
            binding.zt.setTextColor(Color.BLACK)
            binding.qq.setTextColor(Color.BLACK)
        }
        binding.zt.setOnClickListener {
            val filtered = allList.filter { it.status == "早退" }
            adapter.setData(filtered)
            binding.all.setTextColor(Color.BLACK)
            binding.nor.setTextColor(Color.BLACK)
            binding.end.setTextColor(Color.BLACK)
            binding.zt.setTextColor(Color.RED)
            binding.qq.setTextColor(Color.BLACK)
        }
        binding.qq.setOnClickListener {
            val filtered = allList.filter { it.status == "缺勤" }
            adapter.setData(filtered)
            binding.all.setTextColor(Color.BLACK)
            binding.nor.setTextColor(Color.BLACK)
            binding.end.setTextColor(Color.BLACK)
            binding.zt.setTextColor(Color.BLACK)
            binding.qq.setTextColor(Color.RED)
        }
    }
    override fun initData() {
        // 监听打卡列表数据
        vm.ExamListSuccess.observe(this) {
            val sorted = it.sortedByDescending { item ->
                item.id?.toString()?.toLongOrNull() ?: 0L
            }
            allList = sorted
            adapter.setData(sorted)
        }
    }
}